package com.source.lightfowl;

import com.alibaba.fastjson.JSONObject;
import com.source.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @Author ZZF
 * @Time 2021/04/11 2:25
 */
@SpringBootTest
public class JwtDemo {

    @Resource
    private JwtUtils jwtUtils;
    @Test
    void contextLoads(){
        //下面是个过期的jwt，之前调试用的，在swagger登录然后复制下来
        //在HTTP请求中是用HttpServletRequest request
        //用String jwt = request.getHeader("Authorization"); 从前端传来的header里获取，
        //postman和swagger和那个doc.html都能发，swagger就是那个锁，postman百度一下发请求头
        //然后用parseJWT(jwt)获取claims，从claims中获取主体subject，subject我主要封装了
        //userId，loginName，roleId，roleName
        //subject是个json字符串，而我传进去的时候是用Map，所以要用json的工具类把它转成map
        //得到map之后你懂的，键值对，键是29行那几个
//        String jwt = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMzYzNDA4MDQyMDQ5MjMyODk2IiwiaWF0IjoxNjE3Mjg1NjU0LCJzdWIiOiJ7XCJwb3BlZG9tc1wiOltcIuiEmuagh-acuuebkeaOp1wiLFwi6KeS6Imy5p2D6ZmQ566h55CGXCIsXCLohJrmoIfmnLrlj5HmlL7nrqHnkIZcIixcIuiEmuagh-acuuS_neS_ruiusOW9leeuoeeQhlwiLFwi6LS05qCH5L-h5oGv566h55CGXCIsXCLnpr3nsbvkv6Hmga_nrqHnkIZcIixcIuiEmuagh-acuuaKpeW6n-iusOW9leeuoeeQhlwiLFwi5bGg5a6w5qGj5Y-j5a6h5qC4XCIsXCLmnaHlvaLnoIHlm57mlLborrDlvZXnrqHnkIZcIixcIuW4guWcuueuoeeQhlwiLFwi5pel5b-X5L-h5oGv566h55CGXCIsXCLohJrmoIfmnLrnrqHnkIZcIixcIueUqOaIt-S_oeaBr-euoeeQhlwiLFwi5oql6K2m5L-h5oGv6K6w5b2VXCIsXCLohJrmoIfmnLrlm57mlLbnrqHnkIZcIixcIuadoeW9oueggeWPkeaUvuiusOW9leeuoeeQhlwiLFwi5Y675ZCR5qGj5Y-j5a6h5qC4XCIsXCLmoaPlj6PnrqHnkIZcIixcIuiEmuagh-acuueUqOaIt-abtOaWsOS_oeaBr-euoeeQhlwiLFwi6KeS6Imy566h55CGXCIsXCLlsaDlrrDluILlnLrlrqHmoLhcIixcIueUqOaIt-inkuiJsueuoeeQhlwiXSxcInJvbGVJZFwiOlwiMTAwMTBcIixcInJvbGVOYW1lXCI6XCLotoXnuqfnrqHnkIblkZhcIixcInVzZXJJZFwiOlwiMTM2MzQwODA0MjA0OTIzMjg5NlwifSIsImV4cCI6MTYxNzI4NzQ1NH0.W2OmNEt0oLHu_oj4w6vcWpAuU2u7JmoIZOYah5k-ZkY";
//        Claims claims = null;
//        try {
//            claims = jwtUtils.parseJWT(jwt);
//            System.out.println(claims);
//            String subject = claims.getSubject();
//            HashMap hashMap = JSONObject.parseObject(subject, HashMap.class);
//            String userId = (String)hashMap.get("userId");
//            String loginName = (String)hashMap.get("loginName");
//            System.out.println(userId);
//            System.out.println(loginName);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//                String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        String time = date.substring(0, 8);
//        System.out.println(time);
    }
}
